package o;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.Executor;

public class fkk<TResult> implements fkd<TResult> {
    private c ʼ;
    private boolean ˊ;
    private fke ˋ;
    private final Object ˎ = new Object();
    private Executor ˏ;
    private boolean ॱ;
    private Deque<fkf> ॱॱ = new ArrayDeque();

    class c implements fkg {
        private fkg ˊ;
        final /* synthetic */ fkk ॱ;

        public void ˎ(fkg o_fkg) {
            synchronized (this.ॱ.ˎ) {
                this.ˊ = o_fkg;
            }
        }

        public void ˋ() {
            this.ॱ.ˎ();
            synchronized (this.ॱ.ˎ) {
                if (this.ˊ != null) {
                    this.ˊ.ˋ();
                }
            }
        }
    }

    public fke ˊ() {
        fke o_fke;
        synchronized (this.ˎ) {
            o_fke = this.ˋ;
        }
        return o_fke;
    }

    public final void ˎ(TResult tResult) {
        synchronized (this.ˎ) {
            if (this.ॱ) {
                return;
            }
            this.ॱॱ.add(new fkm(tResult));
            this.ˎ.notifyAll();
            ˋ();
        }
    }

    public final void ˎ(fkg o_fkg) {
        synchronized (this.ˎ) {
            if (this.ˊ) {
                o_fkg.ˋ();
                return;
            }
            if (this.ʼ != null) {
                this.ʼ.ˎ(o_fkg);
            }
            this.ॱॱ.add(new fkj(o_fkg));
            this.ˎ.notifyAll();
            ˋ();
        }
    }

    public final void ॱ() {
        synchronized (this.ˎ) {
            if (this.ॱ) {
                return;
            }
            this.ॱ = true;
            this.ॱॱ.add(new fkl());
            this.ˎ.notifyAll();
            ˋ();
        }
    }

    public final void ॱ(Exception exception) {
        synchronized (this.ˎ) {
            if (this.ॱ) {
                return;
            }
            this.ॱ = true;
            this.ॱॱ.add(new fki(exception));
            this.ˎ.notifyAll();
            ˋ();
        }
    }

    private void ˋ() {
        synchronized (this.ˎ) {
            if (this.ˋ == null) {
                return;
            }
            while (true) {
                fkf o_fkf = (fkf) this.ॱॱ.poll();
                if (o_fkf != null) {
                    try {
                        ˊ(this, o_fkf, this.ˏ);
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Throwable e2) {
                        throw new RuntimeException(e2);
                    }
                }
                return;
            }
        }
    }

    private void ˊ(final fkk o_fkk, final fkf o_fkf, Executor executor) {
        executor.execute(new Runnable(this) {
            final /* synthetic */ fkk ˏ;

            public void run() {
                o_fkf.ˋ(o_fkk.ˊ());
            }
        });
    }

    private void ˋ(fke o_fke, Executor executor) {
        synchronized (this.ˎ) {
            if (this.ˋ == null) {
                this.ˋ = o_fke;
                this.ˏ = executor;
                this.ˎ.notifyAll();
                ˋ();
            } else if (o_fke != this.ˋ) {
                throw new IllegalStateException("An observer has already been subscribed on this TaskStream");
            }
        }
    }

    public void ˏ(fke<TResult> o_fke_TResult) {
        ˋ((fke) o_fke_TResult, fjt.ˎ());
    }

    public void ˋ(Executor executor, fke<TResult> o_fke_TResult) {
        ˋ((fke) o_fke_TResult, executor);
    }

    public void ˎ() {
        synchronized (this.ˎ) {
            if (!this.ˊ) {
                this.ॱ = true;
                this.ˊ = true;
                this.ॱॱ.clear();
                this.ˋ = null;
                this.ˏ = null;
                this.ˎ.notifyAll();
            }
        }
    }
}
